package com.example.jetpackcomposebasic.todo

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Preview
@Composable
fun PreviewInputBar() {

    InputBar {

    }
}


@Composable
fun InputBar(add: (ToDoItem) -> Unit) {

    var title: String by remember {
        mutableStateOf("")
    }



    BasicTextField(
        modifier = Modifier.fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 15.dp),
        value = title, onValueChange = {
        if (it.endsWith("\n")) {
            add(ToDoItem(title, Icons.Default.Edit))
            title = ""
        } else {
            title = it
        }
    }, decorationBox = { innerTextField ->

        Box {

            innerTextField()
            IconButton(
                onClick = { title = "" },
                enabled = title.isNotEmpty(),
                modifier = Modifier.align(alignment = Alignment.CenterEnd)
            ) {
                Icon(imageVector = Icons.Default.Delete, contentDescription = null)

            }
        }
    })
}